package com.xxx.kjds.controller;


import com.xxx.kjds.entity.Goods;
import com.xxx.kjds.entity.WishGoodsKey;
import com.xxx.kjds.entity.WishList;
import com.xxx.kjds.service.GoodsService;
import com.xxx.kjds.service.WishGoodsService;
import com.xxx.kjds.service.WishlistService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/wishlist")
public class WishlistController {
    @Autowired
    private WishlistService wishlistService;
    @Autowired
    private WishGoodsService wishGoodsService;
    @Autowired
    private GoodsService goodsService;

    @PostMapping(value = "/myWishGoods")
    @ResponseBody
    public List<Goods> myWishGoods(HttpServletRequest request) {
        List<Goods> resultlist = new ArrayList<>();

        Integer uid = Integer.valueOf(request.getParameter("uid"));
        //根据uid遍历wishlist，是否已存在心愿单
        WishList WishList = wishlistService.selectByUid(uid);
        if (WishList == null) {
            WishList WishList1 = new WishList();
            WishList1.setId(uid);
            WishList1.setBvoId(uid);
            wishlistService.insert(WishList1);
        }
        List<WishGoodsKey> list = wishGoodsService.selectByWid(uid);
        for (WishGoodsKey wishGoodsKey : list) {
            Goods goods = goodsService.selectByPrimaryKey(wishGoodsKey.getGoodsId());
            resultlist.add(goods);
        }
        return resultlist;
    }


    @RequestMapping(value = "/myWishGoodsByPage")
    @ResponseBody
    public List<Goods> myWishGoodsByPage(HttpServletRequest request) {
        List<Goods> resultlist = new ArrayList<>();

        Integer uid = Integer.valueOf(request.getParameter("uid"));
        //根据uid遍历wishlist，是否已存在心愿单
        WishList WishList = wishlistService.selectByUid(uid);
        if (WishList == null) {
            WishList WishList1 = new WishList();
            WishList1.setId(uid);
            WishList1.setBvoId(uid);
            wishlistService.insert(WishList1);
        }
        List<WishGoodsKey> list = wishGoodsService.selectByWid(uid);
        for (WishGoodsKey wishGoodsKey : list) {
            Goods goods = goodsService.selectByPrimaryKey(wishGoodsKey.getGoodsId());
            resultlist.add(goods);
        }
        Integer pageNo = Integer.valueOf(request.getParameter("pageNum"));
        Integer pageSize = Integer.valueOf(request.getParameter("pageSize"));
        List<Goods> result = new ArrayList();
        for(int i = 0; i < list.size(); ++i) {
            if (i >= (pageNo - 1) * pageSize && i < pageNo * pageSize) {
                result.add(resultlist.get(i));
            }
        }
        return result;

    }

    @PostMapping(value = "/deleteGoods")
    @ResponseBody
    public String deleteGoods(HttpServletRequest request) {
        Integer uid = Integer.valueOf(request.getParameter("uid"));
        Integer gid = Integer.valueOf(request.getParameter("gid"));
        WishGoodsKey WishGoodsKey = new WishGoodsKey();
        WishGoodsKey.setWishId(uid);
        WishGoodsKey.setGoodsId(gid);
        int result = wishGoodsService.deleteByPrimaryKey(WishGoodsKey);
        if (result > 0) {
            return "删除成功";
        }
        return "删除失败";
    }
}
